package com.tchepannou.limbe.gae.converter;

import com.google.appengine.api.datastore.Link;
import com.tchepannou.util.converter.Converter;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

/**
 * User: herve
 * Date: 13-06-30 6:13 AM
 */
public class LinkConverter
    implements Converter
{
        //-- Converter overrides
        @Override
        public Object convert(Object value)
        {
            if (value instanceof String)
            {
                try
                {
                    URL url = new URL((String)value);
                    return new Link(url.toString());
                }
                catch (MalformedURLException e)
                {
                    return null;
                }
            }
            else if (value instanceof URL || value instanceof URI)
            {
               return new Link(value.toString());
            }
            else
            {
                return null;
            }
        }
}
